﻿using MesBSW.DAL.MesDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesBSW.BLL.Mes
{
    public class MaterialControler : BaseControler
    {
        /// <summary>
        /// 初始化物料分类树节点
        /// </summary>
        /// <param name="groupList"></param>
        /// <returns></returns>
        public TreeNode InitMaterialGroupTreeNode(List<MatGroup> groupList)
        {
            TreeNode parentNode = new TreeNode();
            parentNode.Text = "BSW(贝斯旺)";
            parentNode.Tag = 1376485;

            this.AddChildNode(parentNode, groupList);
            return parentNode;
        }

        /// <summary>
        /// 为父节点添加子节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="childList"></param>
        public void AddChildNode(TreeNode parentNode, List<MatGroup> groupList)
        {
            if (groupList != null && groupList.Count > 0 && parentNode != null && parentNode.Tag != null)
            {
                int tmpID = int.TryParse(parentNode.Tag.ToString(), out int tmpInt) ? tmpInt : 0;
                var childQuery = groupList.Where(q => q.parent_id == tmpID);
                if (childQuery != null && childQuery.Count() > 0)
                {
                    foreach (MatGroup child in childQuery)
                    {
                        TreeNode childNode = new TreeNode
                        {
                            Text = $"{child.group_number}({child.group_name})",
                            Tag = child.group_id
                        };
                        parentNode.Nodes.Add(childNode);
                        AddChildNode(childNode, groupList);
                    }
                }
            }
        }

        //查询MES系统中的所有物料信息
        public List<MatInfo> SearchMesMaterialInfo()
        {
            List<MatInfo> resList = new List<MatInfo>();
            using (MesDB ctx = new MesDB())
            {
                var query = ctx.MatInfos.AsQueryable();
                resList = query != null && query.Count() > 0 ? query.ToList<MatInfo>() : new List<MatInfo>();
            }
            return resList;
        }

        /// <summary>
        /// 查询Mes系统中所有的物料分类信息
        /// </summary>
        /// <returns></returns>
        public List<MatGroup> SearchMesMaterialGroup()
        {
            List<MatGroup> resList = new List<MatGroup>();
            using (MesDB ctx = new MesDB())
            {
                var query = ctx.MatGroups.AsQueryable();
                resList = query != null && query.Count() > 0 ? query.ToList<MatGroup>() : new List<MatGroup>(); 
            }
            return resList;
        }


        /// <summary>
        /// 将物料分类的物料信息从金蝶系统导入到MES系统
        /// </summary>
        /// <param name="groupList"></param>
        /// <param name="matList"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int SaveMaterialAndGroup(List<MatGroup> groupList,List<MatInfo> matList)
        {
            int count = 0;
            using (MesDB ctx = new MesDB())
            {
                if (groupList != null && groupList.Count > 0)
                {
                    var tmpList = groupList.Select(q => q.group_id).Distinct().ToList<int>();
                    var query = ctx.MatGroups.Where(q => tmpList.Contains(q.group_id));
                    if (query != null && query.Count() > 0)
                    {
                        throw new Exception($"分类：{query.First().group_number}已经存在于MES系统中，保存失败！");
                    }
                }

                if (matList != null && matList.Count > 0)
                {
                    var tmpList = matList.Select(q => q.mat_id).Distinct().ToList<int>();
                    var query = ctx.MatInfos.Where(q => tmpList.Contains(q.mat_id));
                    if (query != null && query.Count() > 0)
                    {
                        throw new Exception($"物料：{query.First().mat_number}已经存在于MES系统中，保存失败！");
                    }
                }

                if (groupList != null && groupList.Count > 0)
                {
                    foreach (var groupBean in groupList)
                    {
                        ctx.MatGroups.Add(groupBean);
                    }
                }

                if (matList != null && matList.Count > 0)
                {
                    foreach (var matBean in matList)
                    {
                        ctx.MatInfos.Add(matBean);
                    }
                }

                count = ctx.SaveChanges();
            }
            return count;
        }


        /// <summary>
        /// 根据条件查询物料信息
        /// </summary>
        /// <param name="groupBean"></param>
        /// <param name="matNumber"></param>
        /// <param name="matName"></param>
        /// <param name="matSpe"></param>
        /// <returns></returns>
        public List<MatInfo> SearchMatInfoForCondition(MatGroup groupBean, string matNumber, string matName, string matSpe)
        {
            List<MatInfo> resList = new List<MatInfo>();
            using (MesDB ctx = new MesDB())
            {
                var query = ctx.MatInfos.AsQueryable();
                if (groupBean != null && groupBean.group_id > 0)
                {
                    query = query.Where(q => q.mat_group == groupBean.group_id);
                }
                if (string.IsNullOrEmpty(matNumber) == false)
                {
                    query = query.Where(q => q.mat_number.Contains(matNumber));
                }
                if (string.IsNullOrEmpty(matName) == false)
                {
                    query = query.Where(q => q.mat_name.Contains(matName));
                }
                if (string.IsNullOrEmpty(matSpe) == false)
                {
                    query = query.Where(q => q.mat_spe.Contains(matSpe));
                }

                resList = query != null && query.Count() > 0 ? query.ToList<MatInfo>() : new List<MatInfo>();
            }
            return resList;
        }


        /// <summary>
        /// 更新物料信息
        /// </summary>
        /// <param name="matList"></param>
        /// <returns></returns>
        public int UpdateMatInfoForBean(List<MatInfo> matList)
        {
            int count = 0;
            using (MesDB ctx = new MesDB())
            {
                if (matList != null && matList.Count > 0)
                {
                    foreach (MatInfo mat in matList)
                    {
                        ctx.MatInfos.Attach(mat);
                        ctx.Entry(mat).State = System.Data.Entity.EntityState.Modified;
                    }
                    count = ctx.SaveChanges();
                }
            }
            return count;
        }
    }
}
